package com.yubest.zuul.exception;

import com.yubest.common.bean.Response;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author hweiyu
 * @Description
 * @Date 2022/4/19 15:12
 */
@RestController
public class MyErrorController implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public Response error(HttpServletRequest request, HttpServletResponse response) {
        //状态码
        Integer code = (Integer) request.getAttribute("javax.servlet.error.status_code");
        //抛出的异常
        Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
        response.setStatus(200);
        //ratelimit限制时，会抛出 429 异常
        if (code == 429) {
            return Response.error("操作过于频繁，请稍后重试！");
        } else {
            return Response.error();
        }

    }
}
